package br.com.j7.jdesk.ui.bean;

import java.util.List;

import br.com.j7.jdesk.library.exception.RepositoryException;
import br.com.j7.jdesk.persistence.entity.Cities;
import br.com.j7.jdesk.persistence.repository.CityRepository;
import br.com.j7.jdesk.persistence.repository.impl.CityRepositoryImpl;

/**
 * 
 * 
 * 
 * @author Edivando Alves
 *
 */
public class CityBean extends WebContext implements Bean{

	private Cities city = new Cities();
	private List<Cities> cities;
	private CityRepository repository;
	
	public CityBean() {
		repository = new CityRepositoryImpl();
	}
	
	
	@Override
	public String adicionar() {
		try {
			if(this.city == null){
				repository.add(this.city);
			}else {
				repository.update(this.city);
			}
			this.city = new Cities();
			this.city = null;
			return "city";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public String remover() {
//		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
//		Integer id = Integer.parseInt(params.get("id"));
//		repository.remove(id);
//		this.cities = null;
		return "city";
	}

	public Cities getCity() {
		return city;
	}
	
	public void setCity(Cities city) {
		this.city = city;
	}
	
	public List<Cities> getCities() {
		try {
			if (cities == null) {
				cities = repository.list();
			}
			return cities;
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}	
	}
	
	@Override
	public String toForm() {
		return "cityForm";
	}
	
	@Override
	public String toList() {
		city = new Cities();
		return "city";
	}

}
